package com.mmall.util;

import org.apache.commons.net.ftp.FTPClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

/**
 * Created by Administrator on 2017/6/11 0011.
 */
public class FTPUtil {

    //创建日志
    private static final Logger logger = LoggerFactory.getLogger(FTPUtil.class);

    private static String ftpIp = PropertiesUtil.getProperty("ftp.server.ip");
    private static String ftpUser = PropertiesUtil.getProperty("ftp.user");
    private static String ftpPassWord = PropertiesUtil.getProperty("ftp.passWord");

    private String ip;
    private String user;
    private String psw;
    private int port;

    // Client:客户；顾客；委托人
    private FTPClient ftpClient;

    public FTPUtil(String ip, String user, String psw, int port) {
        this.ip = ip;
        this.user = user;
        this.psw = psw;
        this.port = port;
    }


    //连接FTP服务器
    private boolean connectServer(String ip, int port, String user, String psw) {
        boolean isSuccess = false;
        ftpClient = new FTPClient();
        try {
            //连接
            ftpClient.connect(ip);
            //登陆
            isSuccess = ftpClient.login(user, psw);
        } catch (IOException e) {
            logger.error("连接FTP服务器异常", e);
        }
        return isSuccess;
    }

    /**
     * 对外开放的方法
     * @param fileList
     * @return
     * @throws IOException
     */
    public static boolean uploadFile(List<File> fileList) throws IOException {
        FTPUtil ftpUtil = new FTPUtil(ftpIp, ftpUser, ftpPassWord, 21);
        logger.info("开始连接FTP服务器");
        boolean result = ftpUtil.uploadFile("img",fileList);
        logger.info("结束上传，上传结果:{}");
        return  result;
    }


    /*ftp服务器在windows下是一个文件夹。
    如果想上传到ftp服务器上的文件夹下一层的文件夹时，就需要用到remotePath
    使我们的上传路径多一些
    */
    private boolean uploadFile(String remotePath, List<File> fileList) throws IOException {
        boolean uploaded = true;
        FileInputStream fileInputStream = null;
        //连接ftp服务器
        if (connectServer(this.ip, this.port, this.user, this.psw)) {
            try {
                //修改工作目录，如果remotePath==null，则不切换工作目录
                ftpClient.changeWorkingDirectory(remotePath);
                //设置缓冲区
                ftpClient.setBufferSize(1024);
                //设置自编码格式
                ftpClient.setControlEncoding("utf-8");
                //把文件类型设置为二进制，防止文件出现乱码
                ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
                //打开本地的被动模式
                ftpClient.enterLocalPassiveMode();
                for (File fileItem:fileList){
                    fileInputStream = new FileInputStream(fileItem);
                    //存储文件到服务器
                    ftpClient.storeFile(fileItem.getName(),fileInputStream);
                }
            } catch (IOException e) {
                logger.error("上传文件异常", e);
                uploaded=false;
            }finally {
                fileInputStream.close();
                ftpClient.disconnect();
            }
        }
        return uploaded;


    }


    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPsw() {
        return psw;
    }

    public void setPsw(String psw) {
        this.psw = psw;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public FTPClient getFtpClient() {
        return ftpClient;
    }

    public void setFtpClient(FTPClient ftpClient) {
        this.ftpClient = ftpClient;
    }


}
